//
//  ElecStartVC.swift
//  SBSafetyApp
//
//  Created by Lina on 2023/11/27.
//
// 【首页-电子台账】电子台账-发起

import Foundation
import UIKit

class ElecStartVC: SubLevelViewController {
    let startV = ElecStartView()
    
    var modal: StandingBookTypeModal!
    var mode: String?
    
    var param: SendHsParam = SendHsParam()
    
    init(withModal _modal: StandingBookTypeModal) {
        super.init(nibName: nil, bundle: nil)
        self.modal = _modal
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        title = "电子台账"
        view.backgroundColor = .bg
        navigationController?.navBarStyle(.white)
        
        if deptName == nil {
            deptName = BSUser.currentUser.deptName
            deptId = BSUser.currentUser.deptId
        }
        
        setupUI()
        
        param.tzlx = modal.typeValue
        param.tzlxmc = modal.remark
        
        startV.reloadUI(withModal: modal)
    }
    
    // MARK: - Actions
    @objc func confirmTapped() {
        if param.jsrid == nil || param.jsrid == "" {
            self.view.showToast(witMsg: "请选择协同人！");
            return;
        }
        
        if param.jsrid == String(BSUser.currentUser.userId) {
            self.view.showToast(witMsg: "协同人不可以选择自己，请重新选择！");
            return;
        }
        
        /// 发起双人会同审批
        do {
            let data: Data = try JSONEncoder().encode(param)
            let string = String(data: data, encoding: String.Encoding.utf8)
            API.postSendHs(withParam: CommonJsonParam(jsonStr: string)) { responseModel in
                DispatchQueue.main.async {
                    if responseModel.errorCode == .Success {
                        self.navigationController?.popViewController(animated: true)
                    }
                }
            }
        } catch  {
            print("postSendHs error", error)
        }
    }
    
    @objc func cancelTapped() {
        navigationController?.popViewController(animated: true)
    }
    
    @objc func cooperateTapped() {
        let listV = PersonListVC()
        listV.didSelectPersonWith = { (person) -> () in
            if let userId = person.userId {
                self.param.jsrid = String(userId)
            }
            self.param.jsrxm = person.nickName
            self.startV.cooperateTF.value = person.nickName
        }
        navigationController?.pushViewController(listV, animated: true)
    }
    
    // MARK: - Setup
    func setupUI() {
        startV.submitBtn.addTarget(self, action: #selector(confirmTapped), for: .touchUpInside)
        startV.cancelBtn.addTarget(self, action: #selector(cancelTapped), for: .touchUpInside)
        startV.cooperateTF.ctl.addTarget(self, action: #selector(cooperateTapped), for: .touchUpInside)
        view.addSubview(startV)
        startV.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(view.safeAreaLayoutGuide.snp.top)
            make.bottom.equalTo(view.safeAreaLayoutGuide.snp.bottom)
        }
    }
}
